package cn.alex.sso.project.service.impl;

import cn.alex.sso.framework.core.PageHelper;
import cn.alex.sso.framework.global.ServiceException;
import cn.alex.sso.project.entity.SysClient;
import cn.alex.sso.project.entity.SysClientCategory;
import cn.alex.sso.project.mapper.SysClientCategoryMapper;
import cn.alex.sso.project.mapper.SysClientMapper;
import cn.alex.sso.project.service.SysClientCategoryService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Alex
 * @description 针对表【sys_client_category(应用分类表)】的数据库操作Service实现
 * @createDate 2024-09-24 17:05:38
 */
@Service
@RequiredArgsConstructor
public class SysClientCategoryServiceImpl extends ServiceImpl<SysClientCategoryMapper, SysClientCategory> implements SysClientCategoryService {
    private final SysClientMapper clientMapper;

    @Override
    public IPage<SysClientCategory> getPage(SysClientCategory entity) {
        return baseMapper.selectPage(PageHelper.getPage(), baseMapper.getQueryWrapper(entity));
    }

    @Override
    public void add(SysClientCategory entity) {
        entity.setCreatedTime(new Date());
        entity.setUpdatedTime(new Date());
        baseMapper.insert(entity);
    }

    @Override
    public void edit(SysClientCategory entity) {
        entity.setUpdatedTime(new Date());
        baseMapper.updateById(entity);
    }

    @Override
    public void del(List<Long> ids) {
        List<SysClient> clientList = clientMapper.selectList(new LambdaQueryWrapper<SysClient>()
                .in(SysClient::getCategoryId, ids)
        );
        if (CollUtil.isNotEmpty(clientList)) {
            throw new ServiceException("该分类下存在应用，无法删除");
        }
        baseMapper.deleteByIds(ids);
    }
}




